package sgd.db.oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import sgd.db.table.Table;

public class DBConnector {

	private String url;
	private String username;
	private String password;

	private Connection conn;

	public DBConnector (String username, String password){
		this.url = "jdbc:oracle:thin:@linux3.dti.supsi.ch:1521:o102";
		this.username = username;
		this.password = password;

		try {
			this.conn = DriverManager.getConnection(this.url, this.username, this.password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<String> select(String fields, String table){
		ArrayList<String> str = new ArrayList<String>(); 
		try {
			Statement stmt = conn.createStatement();
			System.out.println("SELECT " + fields + " FROM " + table + ";");
			ResultSet rs = stmt.executeQuery("SELECT " + fields + " FROM " + table);
			while(rs.next()) {
				str.add(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + "\n");
			}
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return str;
	}

	public boolean insert(String fields, String table, String values){
		try {
			Statement stmt = conn.createStatement();
			System.out.println("INSERT INTO " + table + " (" + fields + ") VALUES " + values + ";");
			stmt.executeUpdate("INSERT INTO " + table + " (" + fields + ") VALUES " + values);
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
